﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Constructor
{
    public partial class Images : UserControl
    {

        public Dictionary<string, PictureBox> LoadedImages { get; set; }
        private PictureBox currPB = null;
        public Images()
        {
            InitializeComponent();
            LoadedImages = new Dictionary<string, PictureBox>();
        }
        private bool LoadImage(string name)
        {
            if (LoadedImages == null) LoadedImages = new Dictionary<string, PictureBox>();
            try
            {
                if (!LoadedImages.ContainsKey(name))
                {
                    Bitmap img = new Bitmap(name);
                    img.Tag = name;
                    PictureBox pb = new PictureBox();
                    pb.BackgroundImage = img;
                    pb.BackgroundImageLayout = ImageLayout.Zoom;
                    pb.Size = new System.Drawing.Size(50, 50);
                    pb.MouseClick += new MouseEventHandler(pb_MouseClick);
                    LoadedImages.Add(name, pb);
                    flowLayoutPanel1.Controls.Add(pb);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        void pb_MouseClick(object sender, MouseEventArgs e)
        {
            if (currPB != null)
            {
                currPB.BorderStyle = System.Windows.Forms.BorderStyle.None;
            }
            currPB = sender as PictureBox;
            currPB.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                contextMenuStrip.Show(this, PointToClient(MousePosition));
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.jpg|*.jpg|*.png|*.png|*.gif|*.gif|All Files|*.*";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string[] images = dlg.FileNames;
                foreach (string image in images)
                    if (!LoadImage(image))
                        MessageBox.Show("Не удалось загрузить изображение - " + image);
            }
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteImage(currPB);
        }

        private void DeleteImage(PictureBox pb)
        {
            if (pb != null && LoadedImages.ContainsKey(pb.BackgroundImage.Tag.ToString()))
            {
                flowLayoutPanel1.Controls.Remove(pb);
                LoadedImages.Remove(pb.BackgroundImage.Tag.ToString());
                currPB = null;
            }
        }
        public void Save(DataSet ds)
        {
            DataTable dt = ds.Tables["images"];
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                DataRow dr = dt.NewRow();
                dr["name"] = (c as PictureBox).BackgroundImage.Tag.ToString();
                dt.Rows.Add(dr);
            }
        }
        public void Load(DataSet ds)
        {
            if (LoadedImages != null)
                LoadedImages.Clear();
            flowLayoutPanel1.Controls.Clear();
            DataTable dt = ds.Tables["images"];
            bool error = true;
            if (dt != null)
                foreach (DataRow dr in dt.Rows)
                {
                    error = error && LoadImage(dr["name"].ToString());
                }
            if (!error)
                MessageBox.Show("Не все изображения удалось загрузить!");
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            DeleteImage(currPB);
        }

    }
}
